<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>EbookAZ</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<?php require "../components/head.php" ?>
</head>
<body onload="MakeCurrentPage(1)">
<div id="container">
	<?php require "../components/guest_menu.php" ?>
	<?php require "../components/guest_header.php" ?>
	<div id="content">
		<?php require "../components/content_left.php"?>
		<div id="content_right" align="center">
		<?php
		
		require_once "Mail.php";
			
			//Dang ki
			 
			if(isset($_REQUEST['txtEmail']))
			{
				include "../common/db.inc";
				$mysqli = new mysqli($host,$username,$password,$dbname);	

				$mysqli->set_charset("utf8");

				if(mysqli_connect_errno())
				{
					echo "Connect failed: ".mysqli_connect_errno();
					return;
				}

				
				$email = $mysqli->real_escape_string($_REQUEST['txtEmail']);
				$userName = $mysqli->real_escape_string($_REQUEST['txtUsername']);
				$pass = $mysqli->real_escape_string($_REQUEST['txtPassword1']);
				$pass = md5($pass);
				$fields = $_REQUEST['checkbox'];
				
					$sql="CALL sp_AddUser('$userName','$pass','$email')";
					$result = @$mysqli->query($sql);
					if ($mysqli->errno) 
					{
						die("Lỗi: Tài khoản này đã tồn tại.");
						$mysqli->close();
					}//end if
					else
					{
						$sql="CALL sp_GetUserByName('$userName')";
						
						$result = $mysqli->query($sql);
						$userid = 0;
						if (!$mysqli->errno) 
						{
							$row = $result->fetch_object();
							$userid = $row->ID;			
							$activekey = $row->ActiveKey;
						}
						else
						{
							$mysqli->close();
							return;
						}//end if	
			
						if($userid == 0)
						{
							echo "Đăng kí thất bại.<br/>";
							echo "<a href='../common/dangky.php'>Trở về trang đăng ký</a>";
						}
						else
						{	
							if(count($fields) > 0)
							{
								foreach($fields as $field)
								{
									$sql = "CALL sp_AddInterest($userid,$field)";
									$mysqli->close();
									$mysqli = new mysqli($host,$username,$password,$dbname);	

									if(!$mysqli->set_charset("utf8"))
									{
										echo "Lỗi load font utf8: ".$mysqli->error;
									}//end if
									

									if(mysqli_connect_errno())
									{
										echo "Connect failed: ".mysqli_connect_errno();
										return;
									}
									$result = $mysqli->query($sql);
									if ($mysqli->errno) 
									{
										echo "Đăng ký thất bại!<br/>";
										echo "<a href='../common/dangky.php'>Trở về trang đăng ký</a>";
										$sql = "CALL sp_DeleteUser($userid)";
										$mysqli->close();
										$mysqli = new mysqli($host,$username,$password,$dbname);
	
										$mysqli->set_charset("utf8");
										
										if(mysqli_connect_errno())
										{
											echo "Connect failed: ".mysqli_connect_errno();
											return;
										}
										$mysqli->query($sql);
										$mysqli->close();
										return;
									}//end if
									
								}//end foreach
							} //end if	 	
							
							$smtphost = "ssl://smtp.gmail.com"; 
							$port = 465; 
							$un = "ebookaz2006@gmail.com";
							$pw = "a23d15h18d24d27";	
				
							$subject = "Ebookaz.com - Thông tin kích hoạt tài khoản - Activation account mail";
							$message = "Xin cám ơn bạn đã đăng ký tài khoảng tại ebookaz.com.\n";
							$message = "Để kích hoạt tài khoản trước khi sử dụng xin bạn vui lòng click vào đường dẫn sau:  ";
							$message .= "http://localhost/ebookaz/common/kichhoat.php?RID=$userid&Key=$activekey";
							$message = str_replace("\n.", "\n..", $message);
							$from = "ebookaz2006@gmail.com";
											
							$headers = array('From'=>$from,'Subject'=>$subject);
							$params = array('host'=>$smtphost,'port'=>$port,'auth'=>true,'username'=>$un,'password'=>$pw);
						
							$smtpMail =& Mail::factory('smtp',$params);
							
							$mail = $smtpMail->send($email,$headers,$message);
							if(PEAR::isError($mail))
							{	
								$mysqli->close();
								$mysqli = new mysqli($host,$username,$password,$dbname);	
								if(mysqli_connect_errno())
								{
									echo "Kết nối thất bại!";
								}
								$sql = "CALL sp_DeleteInterest($userid)";
								$result = $mysqli->query($sql);
								
								$mysqli->close();
								$mysqli = new mysqli($host,$username,$password,$dbname);	
								if(mysqli_connect_errno())
								{
									echo "Kết nối thất bại!";
								}
								$sql = "CALL sp_DeleteUser($userid)";
								$result = $mysqli->query($sql);
								echo "Không thể gửi mail!<br/>";
								echo "<a href='../common/dangky.php'>Trở về trang đăng ký</a>";
							}
							else
							{
								echo "Đăng kí thành công!<br/>";
								echo "Vui lòng kiểm tra email để kích hoạt tài khoản!<br/>";
								echo "<a href='../common/home.php'>Trở về trang chủ</a></a>";
							}//end if
							
						}//end if
					}//end if
				
				$mysqli->close();
			}//end if
			
			//End Dang ki 
			
		
			function getUserId($name)
			{
				$id = 0;
				include "db.inc";
				$mysqli = new mysqli($host,$username,$password,$dbname);
				$mysqli->set_charset("utf8");
				if(mysqli_connect_errno())
				{
					echo "Connect failed: ".mysqli_connect_errno();
					exit();
				}
				else
				{
					$sql="CALL sp_GetUserByName('$name')";
					$result = $mysqli->query($sql);
					if ($mysqli->errno) 
					{
						die($mysqli->errno.": ".$mysqli->error);
					}
					else
					{
						$row = $result->fetch_object();
						$id = $row->ID;			
					}//end if
				}
				$mysqli->close();
				return $id;
			}//end function getUserId
		
			
        ?>
		</div>
	</div>
	<?php require "../components/footer.php" ?>
</div>
</body>
</html>
